//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.ContractInfo;
import com.aidong.model.wills.ContractSign;
import com.aidong.model.wills.MemberChangeShop;
import com.aidong.model.wills.MemberContract;
import com.aidong.model.wills.PayList;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class MemberChangeShopDao extends BaseDao<MemberChangeShop, Integer> {
	public MemberChangeShopDao() {
	}

	public void queryPageList(Pagination<ContractInfo> pg, ConditionMap cd) {
		ArrayList params = new ArrayList();
		String sqlw = cd.toSql(params);
		String sql = "select c.saleId, c.shopCode, c.saleDate, c.contractCode, c.memberCode, i.chName memberName, c.cardCode\n, c.status, c.addFee, c.payAmount, k.beginTime, k.endTime, k.cardType\n, c.newShopCode, c.newContractCode, c.salesCode, c.salesCode2\nfrom MemberChangeShop c inner join MemberContract k on c.contractCode=k.contractCode\ninner join MemberInfo i on c.memberCode=i.memberCode\n" + (sqlw.length() > 0?"where " + sqlw:"") + " order by c.saleDate desc";
		this.queryPageListBySql(ContractInfo.class, pg, sql, params.toArray());
	}

	public void addChangeShop(MemberChangeShop changeShop, List<PayList> payList, MemberContract newContract) {
		String shopCode = newContract.getShopCode();
		String sql = "select max(contractCode) from MemberContract where shopCode=:p1 and contractCode like :p2";
		String maxCode = (String)this.getBySql(String.class, sql, new Object[]{shopCode, shopCode + "%"});
		int maxId = 0;
		if(maxCode != null && maxCode.length() > shopCode.length()) {
			try {
				maxId = Integer.parseInt(maxCode.substring(shopCode.length()));
			} catch (Exception var11) {
				;
			}
		}

		String contractCode = "000000" + (maxId + 1);
		contractCode = shopCode + contractCode.substring(contractCode.length() - 6);
		changeShop.setNewContractCode(contractCode);
		this.add(changeShop);
		Iterator memberName = payList.iterator();

		while(memberName.hasNext()) {
			PayList sign = (PayList)memberName.next();
			sign.setSaleId(changeShop.getSaleId());
			sign.setContractCode(changeShop.getContractCode());
			this.getSession().save(sign);
			if("S011".equalsIgnoreCase(sign.getPayType())) {
				sql = "update PayAli set status=:p1, payListId=:p2, salesCode=:p3, salesCode2=:p4, updateTime=:p5 where barCode=:p6";
				this.updateBySql(sql, new Object[]{Integer.valueOf(2), sign.getAutoId(), sign.getSalesCode(), sign.getSalesCode2(), new Date(), sign.getNote()});
			}
		}

		sql = "update MemberContract set effectiveDate=:p1, effectiveType=:p2, outAmount=:p3, updateTime=:p4 where contractCode=:p5";
		this.updateBySql(sql, new Object[]{newContract.getBeginTime(), "5", newContract.getInAmount(), new Date(), changeShop.getContractCode()});
		newContract.setContractCode(contractCode);
		this.getSession().save(newContract);
		sql = "insert into ContractMember (ContractCode, MemberCode) values (:p1, :p2)";
		this.updateBySql(sql, new Object[]{contractCode, changeShop.getMemberCode()});
		if(changeShop.getEarnestId() != null) {
			sql = "update MemberEarnest set status=1, useSaleId=:p1, useDate=getdate() where saleId=:p2";
			this.updateBySql(sql, new Object[]{changeShop.getSaleId(), changeShop.getEarnestId()});
		}

		String memberName1 = (String)this.getBySql(String.class, "select chName from MemberInfo where memberCode=:p1", new Object[]{changeShop.getMemberCode()});
		ContractSign sign1 = new ContractSign();
		sign1.setShopCode(changeShop.getShopCode());
		sign1.setContractCode(changeShop.getContractCode());
		sign1.setContractType("06");
		sign1.setSaleId(changeShop.getSaleId());
		sign1.setMemberCode(changeShop.getMemberCode());
		sign1.setMemberName(memberName1 == null?"":memberName1);
		sign1.setStatus(Integer.valueOf(1));
		sign1.setSalesCode(changeShop.getSalesCode());
		sign1.setSalesCode2(changeShop.getSalesCode2());
		sign1.setCreateUser(changeShop.getCreateUser());
		sign1.setCreateTime(new Date());
		sign1.setUpdateTime(new Date());
		this.getSession().save(sign1);
	}
}
